
package edu.faytechcc.csc.gui;

import edu.faytechcc.csc.business.Model;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Represents the portion of the main GUI that retrieve information from the 
 * user about the Alice class files to be used in the batch process.
 * 
 * @author Charles Bryan
 */
public class AlicePanel extends JPanel
{

    private JRadioButton singleButton;
    private JRadioButton multipleButton;
    private JRadioButton directoryButton;
    private JCheckBox subDirsCheckBox;
    private JCheckBox keepStructureCheckBox;
    private ButtonGroup buttonGroup;
    private FileChooserPanel classChooserPanel;
    private FileChooserPanel fileChooserPanel;
    private FileChooserPanel folderChooserPanel;
    private JPanel innerPanel;

    public AlicePanel()
    {
        innerPanel = new JPanel();
        innerPanel.setBorder(BorderFactory.createTitledBorder("Alice Class Information"));
        innerPanel.setLayout(new GridLayout(7, 1));
        this.add(innerPanel);
        buildPanel();
    }

    /**
     * Returns an empty string and sets the correct field in the Model, model
     * if the user has entered correct data, otherwise, this method returns 
     * a String with an error message in it.
     * 
     * @param model The Model to set the fields in
     * @return Empty string if all goes well, an error message as a String if 
     * not.
     */
    public String getText(Model model)
    {
        String result = "";
        if (singleButton.isSelected())
        {
            if (this.classChooserPanel.isEmpty())
            {
                result += "Single Alice Class Feild is empty.\n";
                this.classChooserPanel.setWarningColor(true);
            }
            else
            {
                try
                {
                    model.setAliceClass(this.classChooserPanel.getText());
                }
                catch (Exception e)
                {
                    result += e.getMessage() + "\n";
                    this.classChooserPanel.setWarningColor(true);
                }
            }
        }
        else if (multipleButton.isSelected())
        {
            if (this.fileChooserPanel.isEmpty())
            {
                result += "Multiple Alice Classes Feild is empty.\n";
                this.fileChooserPanel.setWarningColor(true);
            }
            else
            {
                try
                {
                    model.setAliceClassFile(this.fileChooserPanel.getText());
                }
                catch (Exception e)
                {
                    result += e.getMessage() + "no message?\n";
                    this.fileChooserPanel.setWarningColor(true);
                }
            }
        }
        else if (directoryButton.isSelected())
        {
            if (this.folderChooserPanel.isEmpty())
            {
                result += "Multiple Alice Classes Feild is empty.\n";
                this.folderChooserPanel.setWarningColor(true);
            }
            else
            {
                try
                {
                    model.setAliceClassDirectory(
                            this.folderChooserPanel.getText(),
                            subDirsCheckBox.isSelected(),
                            keepStructureCheckBox.isSelected());
                }
                catch (Exception e)
                {
                    result += e.getMessage() + "\n";
                    this.folderChooserPanel.setWarningColor(true);
                }
            }
        }
        return result;
    }

    /**
     * Sets up the components for the GUI
     */
    private void buildPanel()
    {
        buttonGroup = new ButtonGroup();

        this.singleButton = new JRadioButton("Single Alice Class", true);
        this.singleButton.addActionListener(new RadioButtonActionListner());
        this.singleButton.setToolTipText("Select if you want to process a "
                + "single Alice class.");
        buttonGroup.add(singleButton);
        innerPanel.add(this.singleButton);

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Alice 2 Class: *.a2c", "a2c");
        classChooserPanel = new FileChooserPanel("File:", 40, filter);
        classChooserPanel.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
        innerPanel.add(classChooserPanel);


        this.multipleButton = new JRadioButton("Multiple Alice Classes");
        this.multipleButton.addActionListener(new RadioButtonActionListner());
        this.multipleButton.setToolTipText("Select if you have a file with"
                + " a list of Alice classes. "
                + "You may create one using the Create Menu bar.");
        buttonGroup.add(multipleButton);
        innerPanel.add(this.multipleButton);

        filter = new FileNameExtensionFilter("Text Files: *.txt *.dat", "txt", "dat");
        fileChooserPanel = new FileChooserPanel("File:", 40, filter);
        fileChooserPanel.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
        fileChooserPanel.setEnabled(false);
        innerPanel.add(fileChooserPanel);

        this.directoryButton = new JRadioButton("Alice Class Directory");
        this.directoryButton.setToolTipText("Select if you want to add all"
                + " the Alice classes in a single directory");
        this.directoryButton.addActionListener(new RadioButtonActionListner());
        buttonGroup.add(directoryButton);
        innerPanel.add(this.directoryButton);

        folderChooserPanel = new FileChooserPanel("File:", 40);
        folderChooserPanel.setFileSectionMode(JFileChooser.DIRECTORIES_ONLY);
        folderChooserPanel.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
        folderChooserPanel.setEnabled(false);
        innerPanel.add(folderChooserPanel);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
        
        subDirsCheckBox = new JCheckBox("Include subdirectories", true);
        subDirsCheckBox.setEnabled(false);
        
        keepStructureCheckBox = new JCheckBox(
                "Keep internal directory structure", true);
        keepStructureCheckBox.setEnabled(false);
                
                
        panel.add(subDirsCheckBox);
        panel.add(keepStructureCheckBox);
        
        innerPanel.add(panel);
    }

    private class RadioButtonActionListner implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (singleButton.isSelected())
            {
                fileChooserPanel.setEnabled(false);
                subDirsCheckBox.setEnabled(false);
                keepStructureCheckBox.setEnabled(false);
                folderChooserPanel.setEnabled(false);
                classChooserPanel.setEnabled(true);
            }
            else if (multipleButton.isSelected())
            {
                fileChooserPanel.setEnabled(true);
                subDirsCheckBox.setEnabled(false);
                keepStructureCheckBox.setEnabled(false);
                folderChooserPanel.setEnabled(false);
                classChooserPanel.setEnabled(false);
            }
            else if (directoryButton.isSelected())
            {
                fileChooserPanel.setEnabled(false);
                subDirsCheckBox.setEnabled(true);
                keepStructureCheckBox.setEnabled(true);
                folderChooserPanel.setEnabled(true);
                classChooserPanel.setEnabled(false);
            }

        }
    }
}
